package com.mg.controller;

import com.github.pagehelper.PageInfo;
import com.mg.common.Constants;
import com.mg.dto.SimpleResult;
import com.mg.dto.UserDto;
import com.mg.model.BankCard;
import com.mg.model.User;
import com.mg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by 技术二科 王永梅 on 2017/3/16.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/admin/selectPageInfo")
    @ResponseBody
    public PageInfo<UserDto> selectPageInfo(UserDto userDto, int limit, int start, HttpServletRequest request) {
        return userService.selectPageInfo(userDto, start, limit);
    }
    @RequestMapping(value = {"/userInfo"}, method = RequestMethod.GET)
    public String regPage() {
        return "/pages/user/userInfo";
    }


    @RequestMapping(value = "/admin/deleteUser")
    @ResponseBody
    public SimpleResult deleteUser(String ids) {
        return userService.deleteUser(ids);
    }

    @RequestMapping(value = "/admin/updateUser")
    @ResponseBody
    public SimpleResult updateUser(String userStr, String bankcards) {
        return userService.updateUser(userStr, bankcards);
    }

    @RequestMapping(value = "/admin/selectUserById")
    @ResponseBody
    public Map<String, Object> selectUserById(Integer id) {
        return userService.selectUserById(id);
    }

    @RequestMapping(value = "/admin/getAllUsers")
    @ResponseBody
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @RequestMapping(value = "/user/getUser")
    @ResponseBody
    public User getUser(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);

        return userService.getUser(user.getId());
    }

    @RequestMapping(value = "/user/saveUser")
    @ResponseBody
    public SimpleResult saveUser(HttpServletRequest request, User user1) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        user1.setId(user.getId());
        return userService.updateUser(user1);
    }
    @RequestMapping(value = "/user/getBankInfo")
    @ResponseBody
    public PageInfo<BankCard> getBankInfo(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        return userService.getBankInfo(user.getId());
    }
    @RequestMapping(value = "/user/saveBankInfo")
    @ResponseBody
    public SimpleResult saveBankInfo(HttpServletRequest request,BankCard bankCard) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);
        bankCard.setUserId(user.getId());
        return userService.saveBankInfo(bankCard);
    }
    @RequestMapping(value = "/user/deleteBankInfo")
    @ResponseBody
    public SimpleResult deleteBankInfo(HttpServletRequest request,String ids) {
        User user = (User) request.getSession().getAttribute(Constants.USER_INFO);

        return userService.deleteBankInfo(ids,user.getId());
    }
}

